package com.konsung.ksdc.manager.shop;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.konsung.ksdc.R;
import com.konsung.ksdc.bean.netbean.OrderProductNetBean;
import com.konsung.ksdc.common.network.RequestCallback;
import com.konsung.ksdc.common.network.RequestUtils;
import com.konsung.ksdc.common.network.ResponseBean;
import com.konsung.ksdc.common.utils.Constants;
import com.konsung.ksdc.common.widget.OnMultiClickListener;
import com.konsung.ksdc.common.widget.WaitingDialog;
import com.konsung.ksdc.manager.appmain.BaseActivity;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 申请退换货
 * Created by DJH on 2017/3/7 0007.
 */

public class ApplyReturnsActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.rl_back)
    RelativeLayout rlBack;
    @Bind(R.id.tab_tv_title)
    TextView tvTitle;
    @Bind(R.id.btn_apply)
    Button btnApply;
    @Bind(R.id.rb_returns)
    RadioButton rbReturns;
    @Bind(R.id.rb_exchange)
    RadioButton rbExchange;
    @Bind(R.id.et_reason)
    EditText etReason;

    private String orderProductId;
    private OrderProductNetBean productNetBean;
    private WaitingDialog dialog;

    @Override
    protected String activityDesc() {
        return getString(R.string.apply_returns_exchange);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply_returns);
        ButterKnife.bind(this);
        initData();
        initView();
    }

    /**
     * 初始化退货申请数据
     */
    private void initData() {
        orderProductId = getIntent().getStringExtra(OrderBuyListAdapter.ORDER_PRODUCT_ID);
        productNetBean = getIntent().getParcelableExtra(OrderBuyListAdapter.ORDER_PRODUCT_BEAN);
    }

    /**
     * 初始化退货申请页面
     */
    private void initView() {
        tvTitle.setText(R.string.apply_returns_exchange);
        rlBack.setOnClickListener(this);
        btnApply.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                submitApplication();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_back:
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 提交退换货申请
     */
    private void submitApplication() {
        String reason = etReason.getText().toString();
        if (!rbReturns.isChecked() && !rbExchange.isChecked()) {
            Toast.makeText(this, R.string.select_returns_type, Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(reason)) {
            Toast.makeText(this, R.string.input_returns_reason, Toast.LENGTH_SHORT).show();
            return;
        }
        dialog = new WaitingDialog(this, "");
        dialog.setText(getString(R.string.submitting));
        dialog.show();
        if (rbReturns.isChecked()) {
            applyReturns(reason);
        } else if (rbExchange.isChecked()) {
            applyExchange(reason);
        }
    }

    /**
     * 申请换货
     * @param reason 换货理由
     */
    private void applyExchange(String reason) {
        RequestUtils.requstExchange(orderProductId, reason, new RequestCallback() {
            @Override
            public void onError(Call call, Exception e) {
                Toast.makeText(ApplyReturnsActivity.this, R.string.tip_server_error, Toast
                        .LENGTH_SHORT).show();
                dialog.dismiss();
            }

            @Override
            public void onResponse(ResponseBean responseBean) {
                if (Constants.REQUEST_OK.equals(responseBean.getResultCode())) {
                    dialog.dismiss();
                    Toast.makeText(ApplyReturnsActivity.this, R.string.tip_request_refund_ok, Toast
                            .LENGTH_SHORT).show();
                    Intent intent = new Intent(ApplyReturnsActivity.this, ReturnsDetailActivity
                            .class);
                    intent.putExtra(OrderBuyListAdapter.ORDER_PRODUCT_ID, orderProductId);
                    intent.putExtra(OrderBuyListAdapter.ORDER_PRODUCT_BEAN, productNetBean);
                    Constants.productStatusChanged = true;
                    startActivity(intent);
                    finish();
                }
            }
        });
    }

    /**
     * 申请退货
     * @param reason 退货理由
     */
    private void applyReturns(String reason) {
        RequestUtils.requstRefund(orderProductId, reason, new RequestCallback() {
            @Override
            public void onError(Call call, Exception e) {
                Toast.makeText(ApplyReturnsActivity.this, R.string.tip_server_error, Toast
                        .LENGTH_SHORT).show();
                dialog.dismiss();
            }

            @Override
            public void onResponse(ResponseBean responseBean) {
                if (Constants.REQUEST_OK.equals(responseBean.getResultCode())) {
                    dialog.dismiss();
                    Toast.makeText(ApplyReturnsActivity.this, R.string.tip_request_refund_ok, Toast
                            .LENGTH_SHORT).show();
                    Intent intent = new Intent(ApplyReturnsActivity.this, ReturnsDetailActivity
                            .class);
                    intent.putExtra(OrderBuyListAdapter.ORDER_PRODUCT_ID, orderProductId);
                    intent.putExtra(OrderBuyListAdapter.ORDER_PRODUCT_BEAN, productNetBean);
                    Constants.productStatusChanged = true;
                    startActivity(intent);
                    finish();
                }
            }
        });
    }
}
